<link rel="stylesheet" type="text/css" href="css/fitem.css">
<script type="text/javascript">
    $.extend($.fn.datebox.defaults,{
        formatter:function(date){
            var y = date.getFullYear();
            var m = date.getMonth()+1;
            var d = date.getDate();
            //return (d<10?('0'+d):d)+'-'+(m<10?('0'+m):m)+'-'+y;
            return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
        },
        parser:function(s){
            if (!s) return new Date();
            var ss = s.split('-');
            var d = parseInt(ss[2],10);
            var m = parseInt(ss[1],10);
            var y = parseInt(ss[0],10);
            if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
                return new Date(y,m-1,d);
            } else {
                return new Date();
            }
        }
    });            

    var url;
    function newInquiry(){
        $('#dlgInquiry').dialog('open').dialog('setTitle','Inquiry Baru');
        $('#fmInquiry').form('clear');
        $('#fmInquiry').form('load',{
            'idCustomer':'1'
        });
        $('#jns_inquiry').val('SVC');
        url = 'pages/inquiry/inquiry_new.php';
    }
    function editInquiry(){
        var row = $('#dgInquiry').datagrid('getSelected');
        if (row){
            $('#dlgInquiry').dialog('open').dialog('setTitle','Edit Inquiry');
            $('#fmInquiry').form('clear');
            $('#fmInquiry').form('load',row);
            if(row.status=='QUCR'){
                $.post('pages/inquiry/inquiry_cek_quotation.php',{noInquiry:row.no_inquiry},function(result){
                    if (result.noQuotation){
                        $('#no_quotation').val(result.noQuotation);
                    } else {
                        document.getElementById('no_quotation').innerHTML='';
                    }
                },'json');
            }
            url = 'pages/inquiry/inquiry_update.php?id='+row.id;
        } else {
            alert('Pilih dulu salah satu data');
        }
    }
    function saveInquiry(){
        $('#fmInquiry').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
                var result = eval('('+result+')');
                if (result.success){
                    $('#dlgInquiry').dialog('close');		// close the dialog
                    $.messager.show({
                        title: 'Sukses',
                        msg: result.msg
                    });
                    $('#dgInquiry').datagrid('reload');	// reload the user data
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: result.msg
                    });
                }
            }
        });
    }
    function removeInquiry(){
        var row = $('#dgInquiry').datagrid('getSelected');
        if (row){
            if(row.status=='OSIN'){
                $.messager.confirm('Confirm','Are you sure you want to remove this inquiry?',function(r){
                    if (r){
                        $.post('pages/inquiry/inquiry_remove.php',{id:row.id},function(result){
                            if (result.success){
                                $('#dgInquiry').datagrid('reload');	// reload the user data
                            } else {
                                $.messager.show({	// show error message
                                    title: 'Error',
                                    msg: result.msg
                                });
                            }
                        },'json');
                    }
                });
            } else {
                $.messager.alert('Error','This Inquiry Cannot be removed.','error');
            }
        } else {
            alert('Pilih dulu salah satu data');
        }
    }
    $(function(){
        $('#idCustomer,#idCustomer1').combogrid({
            panelWidth:300,
            idField:'idCustomer',
            textField:'namaCustomer',
            url:'pages/getCombogridData.php?field=customer',
            mode:'remote',
            fitColumns:true,
            columns:[[
                    {field:'idCustomer',title:'ID Customer',width:60},
                    {field:'namaCustomer',title:'Nama Customer',width:60}
                ]]
        });
    });
    function doSearchDataInquiry() 
    {
        var postThis = $('#fcariInquiry').serialize();
        $('#dgInquiry').datagrid('reload',{
            ref:postThis
        });
    }

</script>

<table id="dgInquiry" title="Daftar Inquiry" class="easyui-datagrid"
       url="pages/inquiry/inquiry_getdata.php"
       toolbar="#toolbarInquiry" pagination="true"
       rownumbers="true" singleSelect="true" >
    <thead>
        <tr>
            <th field="no_inquiry" width="150">No. Inquiry</th>
            <th field="text" width="150">Short Text</th>
            <th field="namaCustomer" width="100">Customer</th>
            <th field="attn" width="100">Attn</th>
            <th field="telp" width="100">Phone</th>
            <th field="fax" width="100">Fax</th>
            <th field="desc" width="150">Description</th>
            <th field="jns_inquiry" width="150">Jenis Pekerjaan</th>
            <th field="status" width="50">Status</th>
            <th field="crtby" width="80">Created By</th>
            <th field="crtdate" width="90">Created Date</th>
            <th field="chgby" width="80">Changed by</th>
            <th field="chgdate" width="90">Changed Date</th>
        </tr>
    </thead>
</table>
<div id="toolbarInquiry" style="padding:5px;height:auto">
    <table>
        <tr>
            <td>
                <form id='fcariInquiry'>	
                    No. Inquiry: 
                    <input class='easyui-validatebox' type='text' name='filternoInquiry' id='filternoinquiry'>
                    Customer: 
                    <input  id="idCustomer1" name="idCustomer1">
                    Tanggal: 
                    <input class="easyui-datebox" 
                           style="width:100px"
                           type="text"
                           name="tanggalsatu"
                           id="tanggalSatu">
                    sampai
                    <input class="easyui-datebox"
                           style="width:100px"
                           type="text"
                           name="tanggaldua"
                           id="tanggalDua">
                    <a href='javascript:void(0)' class='easyui-linkbutton' iconCls='icon-search' onclick='doSearchDataInquiry()'>Cari Data</a>
                    <br><br>
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" onclick="newInquiry()">New</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" onclick="editInquiry()">Edit</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeInquiry()">Remove</a>
                </form>
            </td>	
        </tr>
    </table>
</div>

<div id="dlgInquiry" class="easyui-dialog" style="width:700px;height:auto;padding:10px;" closed="true" modal="true">
    <form id="fmInquiry" method="post">
        <table>
            <tr id="label-noinquiry">
                <td>Inquiry</td>
                <td><input type="text" style="width:280px" name="no_inquiry" id="no_inquiry" placeholder="%00000000000001" readonly></td>
            </tr>
            <tr>
                <td>Short Text</td>
                <td><input class="easyui-validatebox" style="width:520px" name="text" required ></td>
            </tr>
            <tr>
                <td>Jenis Pekerjaan</td>
                <td><select id="jns_inquiry" name="jns_inquiry" required >
                        <option value="SVC">SVC - Service</option>
                        <option value="P">P - Project</option>
                    </select></td>
            </tr>
            <tr id="label-statusinquiry">
                <td>Status</td>
                <td><input class="easyui-validatebox" style="width:280px" name="status" placeholder="OSIN" readonly ></td>
            </tr>
            <tr id="label-noquotation">
                <td>Quotation</td>
                <td><input class="easyui-validatebox" style="width:280px" name="quotation" id="no_quotation" readonly ></td>
            </tr>
        </table>
        <hr>
        <table>
            <tr>
                <td>Customer Name</td>
                <td><input id="idCustomer" name="idCustomer" required ></td>
            </tr>
            <tr>
                <td>Attn</td>
                <td><input class="easyui-validatebox" style="width:180px" name="attn" required ></td>
            </tr>
            <tr>
                <td>Telp</td>
                <td><input class="easyui-validatebox" style="width:180px" name="telp" required ></td>
            </tr>
            <tr>
                <td>Fax</td>
                <td><input class="easyui-validatebox" style="width:180px" name="fax" required ></td>
            </tr>
            <tr>
                <td>Description</td>
                <td><textarea class="easyui-validatebox" name="desc" style="height:90px;width:520px" required></textarea></td>
            </tr>
            <tr>
                <td><a href="#" class="easyui-linkbutton" onclick="saveInquiry()">Save</a></td>
            </tr>
        </table>
    </form>
</div>
